package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

/**
 * Created with Android studio
 *
 * @Author: EDGClearlove7
 * @Date: 2021/08/10/11:38
 * @Description:
 */
public class HomeFragment extends Fragment {
    private View root;
    private int code=0;
    private ArrayList<Fragment> fragments=new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root=inflater.inflate(R.layout.fragment_home,container,false);
        code=1;
        return root;
    }

    @Override
    public void onStart() {
        super.onStart();
        if(root!=null){
            if(code==1){
                TabLayout tabLayout=getView().findViewById(R.id.tab_layout);
                ViewPager2 viewPager2=getView().findViewById(R.id.home_vp);
                fragments.add(new DashboardFragment());
                fragments.add(new DashboardFragment());
                fragments.add(new DashboardFragment());
                PagerAdapter adapter=new PagerAdapter(getActivity(),fragments);
                viewPager2.setAdapter(adapter);
                new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        tab.setText("页数"+position);
                    }
                }).attach();
                viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
                    @Override
                    public void onPageSelected(int position) {
                        viewPager2.setCurrentItem(position);
                    }
                });
            }
        }
    }
}
